﻿using Coolzon.UI.Charts.Widgets.Properties;
using Coolzon.UI.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Visifire.Charts;
using RT = Coolzon.Metadata.Runtime;

namespace Coolzon.UI.Charts.Widgets.Convertors
{
    public class ChartValueTypesConverter : ValueConverter
    {
        public object Convert(ConvertContext context, RT.Property property)
        {
            SeriesYAxisType syt = new SeriesYAxisType();
            string valueTypesString = string.IsNullOrEmpty(property.ValueString) == false ? property.ValueString : property.ValueNode.GetAtrributeValue("Value");
            ChartValueTypes value = ChartValueTypes.Auto;
            if (Enum.TryParse<ChartValueTypes>(valueTypesString, out value))
            {
                return value;
            }
            return IntervalTypes.Auto;
        }

        public RT.ValueNode ConvertBack(ConvertContext context, object value, object designTimeData)
        {
            SeriesYAxisType syt = (SeriesYAxisType)value;
            if (syt == null)
            {
                return null;
            }
            RT.ValueNode valueNode = new RT.ValueNode("SeriesYAxisType");
            valueNode.Attributes = new Dictionary<string, string>();
            string XSeriesYAxisType = string.Empty;
            foreach (var item in syt)
            {
                XSeriesYAxisType += string.Format("{0}:{1}," + item.Key, item.Value);
            }
            XSeriesYAxisType = XSeriesYAxisType.TrimEnd(',');
            valueNode.Attributes.Add("Value", XSeriesYAxisType.ToString());

            return valueNode;
        }
    }
}
